<template>
  <view>
    <uv-status-bar />
    <g-tabs :tabs="tabs" :active="active" status-bar @change="changeTab" />
    <Schedule
      v-if="active === 0"
      :refresh="refresh"
      :onMore="onMore"
      @onRefresh="(v) => emit('update:refresh', v)"
      @onReach="(v) => emit('update:onMore', v)"
    />
    <Untreated
      v-else-if="active === 1"
      :refresh="refresh"
      @count="handleCount"
      @onRefresh="(v) => emit('update:refresh', v)"
    />
    <Attendance
      v-else-if="active === 2"
      :refresh="refresh"
      :onMore="onMore"
      @onRefresh="(v) => emit('update:refresh', v)"
      @onReach="(v) => emit('update:onMore', v)"
    />
  </view>
</template>
<script setup>
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { getStatistics } from "@/request/home";
import Schedule from "@/pages/Home/cp/Schedule.vue";
import Untreated from "@/pages/Home/cp/Untreated.vue";
import Attendance from "@/pages/Home/cp/Attendance.vue";

const props = defineProps({
  // 是否下拉刷新
  refresh: {
    type: Boolean,
    default: false,
  },
  // 上拉加载
  onMore: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(["update:refresh", "update:onMore", "count"]);

const active = ref(-1); // tab下标
const tabs = ref([
  { label: "我的日程", badge: { value: 0 } },
  { label: "待办事项", badge: { value: 0 } },
  { label: "打卡动态", badge: { value: 0 } },
]);

onLoad((e) => {
  setTimeout(() => {
    if ("type" in e) {
      active.value = Number(e.type);
    } else {
      active.value = 0;
    }
    getUnTaskCount();
  }, 0);
});

// tab切换
const changeTab = (val) => {
  active.value = val.index;
  uni.stopPullDownRefresh();
};

// 统计数量
const handleCount = (val) => {
  const index = tabs.value.findIndex((i) => i.label == val.label);
  if (index != -1) {
    tabs.value[index].badge.value = val.count;
  }
};

// 获取待办任务数量
const getUnTaskCount = () => {
  getStatistics().then((res) => {
    if (res.code == 200) {
      const { handleWorkOrderNum } = res.data;
      tabs.value[1].badge.value = handleWorkOrderNum;
    }
  });
};
</script>
<style lang="scss" scoped></style>
